<template>
  <div class="bookmark-item">
    <holder-outlined class="bookmark-drag-handle" />
    <div class="bookmark-content">
      <div class="bookmark-header">
        <img :src="getFavicon(bookmark.url)" :alt="bookmark.title" class="bookmark-icon" @error="handleIconError" @load="handleIconLoad" />
        <div class="bookmark-title" @click="openUrl(bookmark.url)">{{ bookmark.title }}</div>
      </div>
      <div class="bookmark-footer">
        <div class="bookmark-description" :title="bookmark.description || bookmark.url">
          {{ bookmark.description || bookmark.url }}
        </div>
      </div>
    </div>
    <div class="bookmark-actions">
      <edit-outlined class="action-icon" @click="$emit('edit', bookmark)" />
      <delete-outlined class="action-icon delete" @click="confirmDelete" />
    </div>
  </div>
</template>

<script setup>
import { EditOutlined, DeleteOutlined, HolderOutlined } from '@ant-design/icons-vue'
import { Modal } from 'ant-design-vue'
import { watch } from 'vue'
import defaultBookmarkIcon from '@/assets/jiazai.png'

const props = defineProps({
  bookmark: {
    type: Object,
    required: true
  }
})

const emit = defineEmits(['edit', 'delete'])

const getFavicon = (url) => {
  try {
    const urlObj = new URL(url)
    return `https://www.google.com/s2/favicons?domain=${urlObj.hostname}&sz=32`
  } catch {
    return defaultBookmarkIcon
  }
}

const handleIconError = (event) => {
  event.target.src = defaultBookmarkIcon
  event.target.style.backgroundColor = '#f0f0f0'
}

const handleIconLoad = (event) => {
  const img = event.target
  if (img.naturalWidth === 16 && img.naturalHeight === 16) {
    img.src = defaultBookmarkIcon
    img.style.backgroundColor = '#f0f0f0'
  }
}

const openUrl = (url) => {
  window.open(url, '_blank')
}

const confirmDelete = () => {
  Modal.confirm({
    title: '确定要删除这个书签吗？',
    content: '此操作不可恢复',
    onOk: () => {
      emit('delete', props.bookmark)
    }
  })
}
</script>

<style scoped>
.bookmark-item {
  background: #fff;
  border-radius: 8px;
  padding: 12px;
  border: 1px solid #e8e8e8;
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  display: flex;
  gap: 8px;
  align-items: flex-start;
  /* height: 72px; */
  box-sizing: border-box;
  position: relative;
  z-index: 1;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.02);
}

.bookmark-item:hover {
  border-color: #344767;
  background: #f8fbff;
  box-shadow: 0 3px 8px rgba(24, 144, 255, 0.08);
  z-index: 2;
}

.bookmark-drag-handle {
  cursor: move;
  color: #999;
  opacity: 0.6;
  font-size: 16px;
  padding: 4px;
  margin: -4px;
  border-radius: 4px;
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  opacity: 0;
  transform: translateX(-4px);
  visibility: hidden;
}

.bookmark-item:hover .bookmark-drag-handle {
  visibility: visible;
  opacity: 0.8;
  transform: translateX(0);
}

.bookmark-drag-handle:hover {
  opacity: 1;
  background: rgba(0, 0, 0, 0.04);
  transform: scale(1.1);
}

.bookmark-content {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
  transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

.bookmark-header {
  display: flex;
  align-items: center;
  gap: 10px;
  transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

.bookmark-icon {
  width: 30px;
  height: 30px;
  flex-shrink: 0;
  border-radius: 50%;
  transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  object-fit: contain;
  background-color: #f5f5f5;
  padding: 3px;
}

.bookmark-icon[src$='default-bookmark.svg'] {
  padding: 5px;
  opacity: 0.6;
}

.bookmark-title {
  font-size: 16px;
  font-weight: 500;
  color: #757b86;
  cursor: pointer;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 1.5;
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  padding: 3px 0;
  padding-right: 50px;
  box-sizing: border-box;
}

.bookmark-title:hover {
  text-decoration: underline;
  color: #344767;
}

.bookmark-footer {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: auto;
  transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

.bookmark-description {
  font-size: 14px;
  color: #999;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  line-height: 1.5;
  transition: color 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  margin-top: 4px;
}

.bookmark-actions {
  display: flex;
  gap: 4px;
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  flex-shrink: 0;
  transform: translateX(4px);
  position: absolute;
  right: 15px;
  top: 15px;
}

.bookmark-item:hover .bookmark-actions .action-icon {
  opacity: 1;
  color: #344767;
}

.action-icon {
  font-size: 14px;
  color: #999;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  transform: scale(1);
}

.action-icon:hover {
  color: #344767;
  background: rgba(24, 144, 255, 0.1);
  transform: scale(1.1);
}

.action-icon.delete:hover {
  color: #ff4d4f;
  background: rgba(255, 77, 79, 0.1);
}
</style>
